# 给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序。 
# 
#  示例: 
# 
#  输入: [0,1,0,3,12]
# 输出: [1,3,12,0,0] 
# 
#  说明: 
# 
#  
#  必须在原数组上操作，不能拷贝额外的数组。 
#  尽量减少操作次数。 
#  
#  Related Topics 数组 双指针 
#  👍 1043 👎 0


from typing import List


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        left, right = 0, 0
        while right < len(nums):
            if nums[right] != 0:
                nums[left], nums[right] = nums[right], nums[left]
                left += 1
            right += 1



# leetcode submit region end(Prohibit modification and deletion)


def log(*args, **kwargs):
    print(*args, **kwargs)

"""
双指针: 找非0元素换到前面
左指针指向非0元素后一个位置, 右指针寻找非0元素与左指针交换
"""
# [0,1,0,3,12]
# 1.0, 0, 3, 12
# 1,3, 0, 0, 12
# 1,3, 12, 0, 0
# python 交换 两个元素
# a,b = b,a 元组解包: 等号右边会组成一个元组, 然后解包后赋值
# 左指针指向非0元素后一个位置, 右指针寻找非0元素与左指针交换
#
#     def moveZeroes(self, nums: List[int]) -> None:
#         """
#         Do not return anything, modify nums in-place instead.
#         """
#     left, right = 0, 0
#     while right < len(nums):
#         if nums[right] != 0:
#             nums[left], nums[right] = nums[right], nums[left]
#             left += 1
#         right += 1
if __name__ == '__main__':
    s = Solution()
    n = [0, 1, 0, 3, 12]
    s.moveZeroes(n)
    assert n == [1, 3, 12, 0, 0], n

    n2 = [1, 2, 3, 0]
    s.moveZeroes(n2)
    assert n2 == [1, 2, 3, 0], n2
